package com.example.litterpainterdemo.home

import android.content.Context
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.LayoutManager
import androidx.recyclerview.widget.RecyclerView.Orientation
import androidx.recyclerview.widget.RecyclerView.Recycler
import kotlin.math.abs

class ScaleLayoutManager (
    context: Context,
    orientation:Int = RecyclerView.HORIZONTAL,
    reverseLayout:Boolean = false
    ):LinearLayoutManager(context, orientation, reverseLayout){


    //子控件布局完成
    override fun onLayoutChildren(recycler: Recycler?, state: RecyclerView.State?) {
        super.onLayoutChildren(recycler, state)
        //缩放功能
        scaleAnim()
    }

    //横向滚动
    override fun scrollHorizontallyBy(
        dx: Int,
        recycler: Recycler?,
        state: RecyclerView.State?
    ): Int {
        //缩放功能
        scaleAnim()
        return super.scrollHorizontallyBy(dx, recycler, state)
    }

    private fun scaleAnim(){
        //计算中心点
        val center = width/2f
        for (i in 0 until childCount){
            val child = getChildAt(i)!!
            //计算控件的中心点
            val childCenter = (child.left + child.right)/2f
            //计算中心点和控件中心点之间的距离
            val distance = abs(center - childCenter)
            //缩放
            val scale = 1 - distance/center * 0.1f
            child.scaleX  = scale
            child.scaleY = scale
        }
    }
}